<!--底部导航组件-->
<template>
  <div class="tab-bar">
    <mt-tabbar fixed v-model="selected">
      <mt-tab-item v-for="item in tabList" :id="item.id" :key="item.name">
        <div @click="itemClick(item)">
          <div :class="'iconfont icon-' + item.icon"></div>
          {{ item.name }}
        </div>
      </mt-tab-item>
    </mt-tabbar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selected: 0,
      tabList: [
        { id: 0, name: "首页", link: "/home", icon: "home" },
        { id: 1, name: "分类", link: "/category", icon: "category" },
        { id: 2, name: "留言", link: "/message", icon: "message" },
        { id: 3, name: "我的", link: "/profile", icon: "profile" },
      ],
    };
  },
  methods: {
    itemClick({ link, id }) {
      this.$router.push(link);
      this.selected = id;
    },
  },
  watch: {
    "$route.path"() {
      let path = this.$route.fullPath;
      let tar = this.tabList.find((item) =>
        new RegExp(`^${item.link}`).test(path)
      );
      this.selected = tar ? tar.id : 0;

      console.log("当前Tab：" + this.selected);
    },
  },
};
</script>

<style scoped>
.tab-bar .mint-tabbar {
  box-shadow: 0 0 7px rgb(0 0 0 / 10%);
}
.mint-tab-item .iconfont {
  font-size: 24px;
}
.tab-bar /deep/ .mint-tab-item-label {
  color: var(--gray);
}
.mint-tabbar > .mint-tab-item.is-selected {
  background-color: transparent;
}
.mint-tab-item.is-selected /deep/ .mint-tab-item-label {
  color: var(--theme);
}
</style>